package com.lipiao.food.Activity;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lipiao.food.FoodStepsBean;
import com.lipiao.food.R;
import com.lipiao.food.StepBean;
import com.lipiao.food.StepRecycleViewAdapter;

import java.util.ArrayList;
import java.util.List;

public class FoodStepsActivity extends AppCompatActivity {

    static FoodStepsBean foodStepsBean;//传递过来的数据
    ImageView ivFoodPic;
    TextView tvFoodName;
    TextView tvTasteInfor;
    TextView tvTechnologyInfor;
    TextView tvTimeInfor;
    TextView tvDifficultInfor;
    ListView lvMaterial;
    RecyclerView rvSteps;

    static int MaterialNum;
    static String[] materialStrArray;
    private static Context context;//获取context

    static StepRecycleViewAdapter stepRecycleViewAdapter;
    static List<StepBean> mList = new ArrayList<>();

    public static Context getContext() {
        return context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_steps);
        initData();
        initView();
        initListener();
    }

//    @Override
//    protected void onStart() {
//        super.onStart();
//        Log.d("FoodStepsActivity", "onStart: ");
//        rvSteps.setHasFixedSize(true);
//        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
//        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//        rvSteps.setLayoutManager(layoutManager);
//        initStepBeanList();
//    }

    //获取转递过来的FoodStepsBean对象12:31:17.057
    private void initData() {
        foodStepsBean = (FoodStepsBean) getIntent().getSerializableExtra("foodStepInfor");

        materialStrArray = foodStepsBean.getFoodMaterials().split("、");
        MaterialNum = materialStrArray.length;//根据食材字符串，分割成数组，长度即个数


    }

    private void initView() {
        //界面组件声明
        ivFoodPic = findViewById(R.id.iv_food_pic);
        tvFoodName = findViewById(R.id.tv_food_title);
        tvTasteInfor = findViewById(R.id.tv_taste_infor);
        tvTechnologyInfor = findViewById(R.id.tv_technology_infor);
        tvTimeInfor = findViewById(R.id.tv_time_infor);
        tvDifficultInfor = findViewById(R.id.tv_difficult_infor);
        lvMaterial = findViewById(R.id.lv_material);
        rvSteps = findViewById(R.id.rv_steps);

        Glide.with(getBaseContext()).load(foodStepsBean.getPicUrl()).into(ivFoodPic);
        tvFoodName.setText(foodStepsBean.getFoodName());
        tvTasteInfor.setText(foodStepsBean.getFoodInforArr()[0]);
        tvTechnologyInfor.setText(foodStepsBean.getFoodInforArr()[1]);
        tvTimeInfor.setText(foodStepsBean.getFoodInforArr()[2]);
        tvDifficultInfor.setText(foodStepsBean.getFoodInforArr()[3]);

        //listView初始化
        //文字需裁剪 第一个和最后一个
        materialStrArray[0] = materialStrArray[0].substring(3);//裁切掉多余的文字 原料:
        materialStrArray[materialStrArray.length - 1] = materialStrArray[materialStrArray.length - 1].replace("。", "");//裁切掉多余的文字 。

        for (int i = 0; i < materialStrArray.length; i++) {
            materialStrArray[i] += ": " + foodStepsBean.getMaterialQuantityList().get(i);
        }
        //为listView设置适配器
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(FoodStepsActivity.this,
                android.R.layout.simple_list_item_1, materialStrArray);
        lvMaterial.setAdapter(adapter);

        // rvSteps   //recycleView初始化
//        foodStepsBean.getStepList();
//        foodStepsBean.getStepPicList();
//
        rvSteps.setHasFixedSize(true);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvSteps.setLayoutManager(layoutManager);
        initStepBeanList();

    }

    private void initStepBeanList() {
        mList = new ArrayList<>();
        //含有网络加载，所以开启新线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                //添加数据
                for (int i = 0; i < foodStepsBean.getStepList().size(); i++) {
                    StepBean element = new StepBean(""+foodStepsBean.getStepPicList().get(i), ""+foodStepsBean.getStepList().get(i));
                    Log.d("FoodStepsActivity", "run: "+foodStepsBean.getStepList().get(i)+foodStepsBean.getStepPicList().get(i));
                    mList.add(element);
                }
                //实例化MyAdapter并传入mList对象
                stepRecycleViewAdapter = new StepRecycleViewAdapter(mList);
                //回到主线程更新UI
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //为RecyclerView对象mRecyclerView设置adapter
                        rvSteps.setAdapter(stepRecycleViewAdapter);
                    }
                });

//                //实例化MyAdapter并传入mList对象
//                stepRecycleViewAdapter = new StepRecycleViewAdapter(mList);
                //为RecyclerView对象mRecyclerView设置adapter
//                rvSteps.setAdapter(stepRecycleViewAdapter);

            }//run
        }).start();//new thread
    }//initFoodBeanList()


    private void initListener() {

    }
}